package l3s.wikirevisions;

import info.bliki.wiki.model.WikiModel;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author pham@l3s.de
 */

import org.apache.solr.client.solrj.beans.Field;

public class Result implements Serializable {
	private static int NUM_ENTITIES_TO_SHOW = 10;
	@Field
	private String page_ID;
	
	@Field
	private Date revision;
	
	@Field
	private Date page_revision;
	
	@Field
	private String ID;
	
	@Field
	private double score;
	
	@Field
	private String original_text;
	
	@Field
	private List<String> entities;	
	
	private List<String> snippets;	
	
	private int entityOccurences;
	
	
	public int getEntityOccurences() {
		return entityOccurences;
	}

	public void setEntityOccurences(int entityOccurences) {
		this.entityOccurences = entityOccurences;
	}

	public List<String> getSnippets() {
		return snippets;
	}

	public void setSnippets(List<String> snippets) {
		this.snippets = snippets;
	}
	
	public String getSnippetsHtml(){
		String str = "";
		if (snippets == null)
			return str;
		for (String s: snippets)
			str += s + "...";
		return str;
		//return WikiModel.toHtml(str);
	}

	public String getOriginal_text() {
		return original_text;
	}
	
	public List<String> getEntities() {
		return entities;
	}

	public void setEntities(List<String> entities) {
		this.entities = entities;
	}

	public String getHtml(){
		//WikiModel wikimodel = new WikiModel("https://en.wikipedia.org/wiki/${image}", "https://en.wikipedia.org/wiki/${title}");
		WikiModel wikimodel = new WikiModel("", "https://en.wikipedia.org/wiki/${title}");
		String htmlText = wikimodel.render(original_text);
		/*
		if (getPageTitle().equals("Joe_Biden"))
		{
			System.out.println("HTML text of " + getPageTitle());
			System.out.println(htmlText);
		}
		*/
		return htmlText;
	}

	public void setOriginal_text(String original_text) {
		this.original_text = original_text;
	}

	public String getPageTitle() {
		if (Utils.getEntities().containsKey(page_ID))
			return Utils.getEntities().get(page_ID);
		return page_ID;
	}	
	
	public String getEntitiesStr(){
		if (entities == null)
			return "";
		
		int limit = NUM_ENTITIES_TO_SHOW;
		if (entities.size() < NUM_ENTITIES_TO_SHOW)
			limit = entities.size();
		String s = "";
		for (int i = 0; i < limit; i++)
			s += entities.get(i) + ", ";
		if (s.length() > 0)
			s += "...";
		
		return s;
	}

	public Date getPage_revision() {
		return page_revision;
	}



	public void setPage_revision(Date page_revision) {
		this.page_revision = page_revision;
	}



	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	public String getPage_ID() {
		return page_ID;
	}

	public void setPage_ID(String page_ID) {
		this.page_ID = page_ID;
	}

	public Date getRevision() {
		if (revision != null)
			return revision;
		return page_revision;
	}
	
	public String getRevisionStr(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		return sdf.format(getRevision());
	}

	public void setRevision(Date revision) {
		this.revision = revision;
	}

	public String getID() {
		return ID;
	}

	public void setID(String iD) {
		ID = iD;
	}

	@Override
	public String toString() {
		return page_ID;
	}
	
	public static void main(String args[]){
		String htmlText = "http://localhost:8080/File:Ann_Dunham_with_father_and_children.jpg";
		htmlText = htmlText.replaceFirst("localhost:\\d*", "wikipedia.org/wiki");
		System.out.println(htmlText);
	}
}
